package com.ht.api.task;

import javax.sql.DataSource;

import com.ht.api.db.Db;
import com.ht.api.db.DbConfig;
import com.ht.api.db.DbList;
import com.ht.api.db.Entity;
import com.ht.api.db.StreamLoadConfig;
import com.ht.api.db.task.TaskHandler;

import cn.hutool.json.JSONObject;

/**
 * 标记客户接替成功的任务
 * @author asq
 * @createTime 2025年3月13日 15:40:40
 */
public class TransferQueryTask implements TaskHandler{

	@Override
	public String getName() {
		return "标记客户接替成功";
	}

	@Override
	public boolean handler(JSONObject requestParam, String groupId, long taskId) {
		// 获取数据源
		long tenantId = requestParam.getLong("tenant_id");
		DataSource ds = DbConfig.useTenantDb(tenantId);
		StreamLoadConfig streamLoadConfig = DbConfig.getStreamLoadConfigByDataSource(ds);


		String transferId = requestParam.getStr("main_id");

		DbList list = Db.createSqlParser("wx_transfer_user_item").select("main_id").where("transfer_id", transferId).query(ds);

		for (Entity entity : list) {
			entity.set("status", 2);
		}

		Entity transferUser = Db.createSqlParser("wx_transfer_user")
				.select("main_id, handover_userid, appid").findById(ds, transferId);
		transferUser.setMainId(transferId);
		transferUser.set("succ_cnt", list.size());

		Entity workuserDimission = Entity.create("wx_workuser_dimission");
		workuserDimission.set("appid", transferUser.getStr("appid"));
		workuserDimission.set("handover_userid", transferUser.getStr("handover_userid"));
		workuserDimission.set("succ_cnt", list.size());

		list.add(transferUser);
		list.add(workuserDimission);

		Db.insertBatchWithStream(streamLoadConfig, list);

		return true;
	}
	
}
